/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2023 AUTHOR,AFFILIATION
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Application
    meshVolume

Description

\*---------------------------------------------------------------------------*/

/**
 * @author tttt
 * @date 2023-11-27
 * @brief Mesh volumn calculation
 *        OFv2306
*/

#include "fvCFD.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

int main(int argc, char *argv[])
{
    #include "setRootCase.H"
    #include "createTime.H"


    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

    // 网格类fvMesh实例化
    #include "createMesh.H"
    // Foam::fvMesh mesh(runTime);// v2306报错

    Info << "You are calculating at time: " << runTime.timeName() << endl;
    // 声明场
    volScalarField vol
    (
        IOobject(
            "vol", 
            runTime.timeName(), 
            mesh, 
            IOobject::NO_READ, 
            IOobject::AUTO_WRITE
            ), 
            mesh, 
            dimensionedScalar("vol", dimVolume, 0.0)
            );


    // 遍历网格计算网格体积分布
    const DimensionedField<scalar,volMesh>& cellVolumes = mesh.V();
    forAll(cellVolumes, cellI)
    {
        vol[cellI] = cellVolumes[cellI];
    }
    
#ifdef ttttDebug
    // 计算网格总的体积
    scalar volTot = gSum(mesh.V());//求解计算域体积，gSum是求和函数（并行适用）
    Info<< "Geometry volume sum: "<< volTot <<endl;//Info可以打印出体积volume的值

    // 调用OpenFOAM不同接口，追溯一下这两个不同接口
    // a) mesh.V()  ----> fvMesh (fvMesh -> polyMesh -> primitiveMesh)
    // b) mesh.cellVolumes() ---> primitiveMesh
    Info << "Volume compare: " << volTot - gSum(mesh.cellVolumes()) << endl;

    #include "GeometricCalc.H"
    /* #include "FieldInOpenFOAM.C" */

#endif
    // 写下体积场
    vol.write();

    Info<< nl;
    runTime.printExecutionTime(Info);

    Info<< "End\n" << endl;

    return 0;
}


// ************************************************************************* //
